package com.cade.agriculturemall.view.personalCenter.selectAddress;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.cade.agriculturemall.base.BasePresenter;
import com.cade.agriculturemall.model.AddressEntity;
import com.cade.agriculturemall.model.UserInfoEntity;

import java.util.List;

/**
 * Created by Lobby on 2017/12/27.
 */

public class AddressSelectPresenter extends BasePresenter<AddressSelectContract.IView> implements AddressSelectContract.IPresenter {
    public AddressSelectPresenter(AddressSelectContract.IView view) {
        super(view);
    }

    @Override
    public void onResume() {

    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void downLoadMyAddress() {
        AVUser.alwaysUseSubUserClass(UserInfoEntity.class);
        UserInfoEntity user = AVUser.getCurrentUser(UserInfoEntity.class);
//        String currentUserId = user.getObjectId();

        AVQuery<AddressEntity> addressEntityAVQuery = new AVQuery<>("user_address");
        addressEntityAVQuery.whereEqualTo("ownerObjectId", user);
        addressEntityAVQuery.orderByAscending("isfirst");
        addressEntityAVQuery.findInBackground(new FindCallback<AddressEntity>() {
            @Override
            public void done(List<AddressEntity> list, AVException e) {
                if (e == null) {
                    mView.getMyAddress(list);
                } else {
//                    mView.setData(null);
                    e.printStackTrace();
                }
            }
        });
    }
}
